import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-13
 * Time: 18:48
 * author:Test01
 */
public class Test01 {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/java_104?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();


        //操作数据库
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        //执行
        ResultSet resultSet = statement.executeQuery();
        while( resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + "name = " + name);
        }

        //关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
